<?php
class StationLetter extends Eloquent {

    protected $fillable = array('subject', 'message');

    public static function getSystemLetters($row_per_page, $offset)
    {
        $user = Auth::user();
        if (! $user)
        {
            return;
        }
        return StationLetter::where('to', '=', $user->id)->OfCreatorId(0)->orderBy('created_at', 'DESC')
            ->take($row_per_page)
            ->skip($offset)
            ->get();
    }

    public static function getUserLetters($creator_id)
    {
        $user = Auth::user();
        if (! $user)
        {
            return;
        }

        return DB::table('station_letters')
            ->orWhere(function($query) use ($user, $creator_id)
            {
                $query->where('to', '=', $user->id)
                    ->where('creator_id', '=', $creator_id);
            })
            ->orWhere(function($query) use ($user, $creator_id)
            {
                $query->where('to', '=', $creator_id)
                    ->where('creator_id', '=', $user->id);
            })
            ->orderBy('created_at', 'DESC')->get();
    }

    public static function sendSystemLetter($subject, $message, User $to)
    {
        $stationLetter = new StationLetter(compact('subject', 'message'));
        $stationLetter->creator_id = 0;
        $stationLetter->to = $to->id;
        $stationLetter->is_read = 0;
        $stationLetter->save();

        return $stationLetter;
    }

    public static function sendUserLetter($message, User $to)
    {
        $user = Auth::user();
        if (! $user OR $user->id == $to->id)
        {
            return;
        }
        $subject = sprintf('%s 给您发的站内信', $user->email);

        $stationLetter = new StationLetter(compact('subject', 'message'));
        $stationLetter->creator_id = $user->id;
        $stationLetter->to = $to->id;
        $stationLetter->is_read = 0;

        DB::transaction(function() use($stationLetter, $to, $user){
            $stationLetter->save();
            StationLetterUser::addLetter($to->id, $user->id);
        });

        return $stationLetter;
    }

    public static function readLetter($id)
    {
        DB::transaction(function() use($id){
            $stationLetter = StationLetter::where('id', '=', $id)->lockForUpdate()->first();

            if ($stationLetter->is_read)
            {
                return;
            }
            $stationLetter->is_read = 1;

            $stationLetter->touch();
            $stationLetter->save();
            if (StationLetter::_isUserLetter($stationLetter))
            {
                StationLetterUser::readLetter($stationLetter->to, $stationLetter->creator_id);
            }
        });
    }

    private static function _isUserLetter($stationLetter)
    {
        return $stationLetter->creator_id > 0;
    }

    public function creatorName()
    {
        if($this->creator_id)
        {
            $user = User::find($this->creator_id);
            $customer = Customer::where('user_id', $this->creator_id)->first();
            if ($customer)
            {
                return $customer->realname ? $customer->realname : $user->email;
            }
            $company = Company::where('user_id', $this->creator_id)->first();
            if ($company)
            {
                return $company->name ? $company->name : $user->email;
            }
            $lawyer = Lawyer::where('user_id', $this->creator_id)->first();
            if ($lawyer)
            {
                return $lawyer->realname ? $lawyer->realname.'律师' : $user->email;
            }

            return $user->email;
        } else {
            return '系统通知';
        }
    }

    public function creatorAvatar()
    {
        return $this->creator_id ? User::find($this->creator_id)->avatar : '';
    }

    public function creator()
    {
        if($this->creator_id)
        {
            $customer = Customer::where('user_id', $this->creator_id)->first();
            if ($customer)
            {
                return $customer;
            }

            $company = Company::where('user_id', $this->creator_id)->first();
            if ($company)
            {
                return $company;
            }

            $lawyer = Lawyer::where('user_id', $this->creator_id)->first();
            if ($lawyer)
            {
                return $lawyer;
            }

            return User::find($this->creator_id);
        }
        else
        {
            return NULL;
        }
    }

    public function scopeOfCreatorId($query, $creator_id)
    {
        $query = $query->whereCreatorId($creator_id);

        return $query;
    }

    
	public static function sendWelcomeLetter($user)
	{
		// 发送站内消息
		$subject = "【通知】账户激活成功";
		$message = "您好，欢迎注册我的律团网！您的账户已成功激活，特送上100积分。为享有更好的服务，请尽快完善您的个人资料，如需帮助，请联系客服。";
		StationLetter::sendSystemLetter($subject, $message, $user);
		return;
	}
}